package com.minazuki.signinserver.service;

import com.minazuki.signinserver.config.JwtProperties;
import com.minazuki.signinserver.pojo.api.UserInfo;
import com.minazuki.signinserver.pojo.dao.Course;
import com.minazuki.signinserver.pojo.dao.CourseMapper;
import com.minazuki.signinserver.pojo.dao.CourseSelect;
import com.minazuki.signinserver.pojo.dao.CourseSelectMapper;
import com.minazuki.signinserver.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseMapper mapper;

    @Autowired
    private CourseSelectMapper selectMapper;
    @Autowired
    JwtProperties jwtProperties;

    public boolean addCourse(Course course, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        course.setId(null);
        course.setCreateTime(new Date());
        course.setUid(info.getId());
        return mapper.insertSelective(course) > 0;
    }

    public List<Course> getCourses(String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Course>();
        }
        Example example = new Example.Builder(Course.class)
                .where(Sqls.custom().andEqualTo("uid", info.getId()))
                .build();
        return mapper.selectByExample(example);
    }

    public boolean delCourse(int id, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        Course course = new Course();
        course.setUid(info.getId());
        course.setId(id);
        int result = mapper.delete(course);
        return result > 0;
    }

    public boolean updateCourse(Course course, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        course.setCreateTime(null);
        Example example = new Example.Builder(Course.class)
                .where(Sqls.custom().andEqualTo("uid", info.getId()).andEqualTo("id", course.getId()))
                .build();
        return mapper.updateByExampleSelective(course, example) > 0;
    }

    public boolean joinCourse(int cid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (mapper.selectCount(new Course(cid)) == 0)//判断课程是否存在
            return false;

        CourseSelect courseSelect = new CourseSelect();
        courseSelect.setCid(cid);
        courseSelect.setUid(info.getId());
        if (selectMapper.selectCount(courseSelect) > 0)
            return false;//判断是否已经选过课

        return selectMapper.insert(courseSelect) > 0;
    }

    public List getCoursesJoin(String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Course>();
        }
        Example csExample = new Example.Builder(CourseSelect.class)
                .where(Sqls.custom().andEqualTo("uid", info.getId()))
                .build();
        List<CourseSelect> courseSelects = selectMapper.selectByExample(csExample);
        if (courseSelects.size() == 0) return new ArrayList();

        ArrayList<Course> courses = new ArrayList<>();

        for (CourseSelect cs : courseSelects) {
            Course course = new Course();
            course.setId(cs.getCid());
            course = mapper.selectOne(course);
            if (course != null)
                courses.add(course);
        }
        return courses;
    }

    public boolean quitCourse(int cid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        CourseSelect courseSelect = new CourseSelect();
        courseSelect.setCid(cid);
        courseSelect.setUid(info.getId());
        return selectMapper.delete(courseSelect) > 0;

    }

    public String queryCourseName(int cid) {
        Course course = new Course(cid);
        course = mapper.selectByPrimaryKey(course);
        if (course == null) return "";
        return course.getName();
    }
}
